//
//  ViewController.m
//  09-省市区tableView
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
#import "CityViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <NSMutableDictionary *> *country;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"省份";
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);

    [self createData];
}
- (void)createData {
    NSString *path = @"/Users/dllo/DLS160710/UI/HomeWork/09-省市区tableView/09-省市区tableView/area.txt";
    NSString *fileStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@",fileStr);
    NSArray *dataArray = [fileStr componentsSeparatedByString:@"\n"];
//    NSLog(@"%@",dataArray);
    self.country = [NSMutableArray array];
    for (NSString *line in dataArray) {
        if (![line hasPrefix:@" "]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            [dic setObject:line forKey:@"省名"];
            [dic setObject:arr forKey:@"市组"];
            [_country addObject:dic];
        } else if (![line hasPrefix:@"   "]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            NSMutableArray *arr = [NSMutableArray array];
            [dic setObject:line forKey:@"市名"];
            [dic setObject:arr forKey:@"区组"];
            NSMutableArray *cityArr = [[_country lastObject] objectForKey:@"市组"];
            [cityArr addObject:dic];
        } else {
            NSMutableArray *cityArr = [[_country lastObject] objectForKey:@"市组"];
            NSMutableArray *zoneArr = [[cityArr lastObject] objectForKey:@"区组"];
            [zoneArr addObject:line];
        }
    }
//    NSLog(@"%@",_country);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CityViewController *cityVC = [CityViewController new];
    [self.navigationController pushViewController:cityVC animated:YES];
    cityVC.provinceDic = _country[indexPath.row];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _country.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    NSString *proName = [_country[indexPath.row] objectForKey:@"省名"];
    NSArray *tempArr = [proName componentsSeparatedByString:@" "];
    cell.textLabel.text = tempArr[0];
    cell.detailTextLabel.text = [tempArr lastObject];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
